﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.IO;

namespace NeonMika.EightTracks.Responses
{
    public class Response
    {
        #region Variables

        int id = -1;
        string path = null;
        string restful_url = null;
        int totalentries = -1;
        int page = -1;
        int perpage = -1;
        int nextpage = -1;
        int previouspage = -1;
        int totalpages = -1;
        string canonical_path;
        string status = null;
        string errors = null;
        string notices = null;
        bool? logged_in = null;
        string name = null;
        string api_warning = null;
        string api_version = null;

        #endregion

        #region Properties

        public string ApiVersion
        {
            get { return api_version; }
            set { api_version = value; }
        }

        public string ApiWarning
        {
            get { return api_warning; }
            set { api_warning = value; }
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        public string Path
        {
            get { return path; }
            set { path = value; }
        }

        public string RestfulUrl
        {
            get { return restful_url; }
            set { restful_url = value; }
        }

        public int TotalEntries
        {
            get { return totalentries; }
            set { totalentries = value; }
        }

        public int Page
        {
            get { return page; }
            set { page = value; }
        }

        public int PerPage
        {
            get { return perpage; }
            set { perpage = value; }
        }


        public int NextPage
        {
            get { return nextpage; }
            set { nextpage = value; }
        }


        public int PreviousPage
        {
            get { return previouspage; }
            set { previouspage = value; }
        }


        public int TotalPages
        {
            get { return totalpages; }
            set { totalpages = value; }
        }

        public string CanonicalPath
        {
            get { return canonical_path; }
            set { canonical_path = value; }
        }


        public string Status
        {
            get { return status; }
            set { status = value; }
        }

        public string Errors
        {
            get { return errors; }
            set { errors = value; }
        }


        public string Notices
        {
            get { return notices; }
            set { notices = value; }
        }


        public bool? LoggedIn
        {
            get { return logged_in; }
            set { logged_in = value; }
        }

        #endregion

        public Response(XElement xml)
        {
            string element = "";

            if ( xml.Element("id") != null && xml.Element("id").Attribute("nil") == null )
                Id = Convert.ToInt32(xml.Element("id").Value);

            if ( xml.Element("total-entries") != null && xml.Element("total-entries").Attribute("nil") == null )
                TotalEntries = Convert.ToInt32(xml.Element("total-entries").Value);

            if ( xml.Element("page") != null && xml.Element("page").Attribute("nil") == null )
                Page = Convert.ToInt32(xml.Element("page").Value);

            if ( xml.Element("per-page") != null && xml.Element("per-page").Attribute("nil") == null )
                PerPage = Convert.ToInt32(xml.Element("per-page").Value);

            if ( xml.Element("next-page") != null && xml.Element("next-page").Attribute("nil") == null )
                NextPage = Convert.ToInt32(xml.Element("next-page").Value);

            if ( xml.Element("previous-page") != null && xml.Element("previous-page").Attribute("nil") == null )
                PreviousPage = Convert.ToInt32(xml.Element("previous-page").Value);

            if ( xml.Element("total-pages") != null && xml.Element("total-pages").Attribute("nil") == null )
                TotalPages = Convert.ToInt32(xml.Element("total-pages").Value);

            if ( xml.Element("status") != null && xml.Element("status").Attribute("nil") == null )
                Status = xml.Element("status").Value.ToString( );

            element = "canonical-path";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                CanonicalPath = xml.Element(element).Value.ToString( );

            element = "errors";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                Errors = xml.Element(element).Value.ToString( );

            element = "notices";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                Notices = xml.Element(element).Value.ToString( );

            element = "path";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                Path = xml.Element(element).Value.ToString( );

            element = "restful-url";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                RestfulUrl = xml.Element(element).Value.ToString( );

            element = "name";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                Name = xml.Element(element).Value.ToString( );

            element = "api-warning";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                ApiWarning = xml.Element(element).Value.ToString( );

            element = "api-version";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                ApiWarning = xml.Element(element).Value.ToString( );

            element = "logged-in";
            if ( xml.Element(element) != null && xml.Element(element).Attribute("nil") == null )
                LoggedIn = xml.Element(element).Value.ToString( ) == "true" ? true : false;
        }
    }
}
